package sort;

public class InsertSort implements Sort{
    @Override
    public void sort(int[] arr, boolean asc) {
        int last = 1, len = arr.length;
        while (last < len) {
            int tmp = arr[last];
            int idx = last;
            // 向前寻找对应的插入位置
            for (; idx > 0; --idx) {
                if ((asc && tmp >= arr[idx - 1]) || (!asc && tmp <= arr[idx - 1])) {
                    break;
                }
                arr[idx] = arr[idx - 1];
            }
            arr[idx] = tmp;
            last++;
        }
    }
}
